package com.igeek.collateral.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.constant.MessageConstant;
import com.igeek.collateral.entity.Menu;
import com.igeek.collateral.entity.Permission;
import com.igeek.collateral.entity.Role;
import com.igeek.collateral.result.Result;
import com.igeek.collateral.service.IMenuService;
import com.igeek.collateral.service.IPermissionService;
import com.igeek.collateral.service.IRoleService;
import com.igeek.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/role")
@Api(tags = "角色接口")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IPermissionService permissionService;


    @GetMapping("findPage")
    @ApiOperation("条件分页查询检查项信息")
    public Result findPage(PageQueryVO vo){
        try {
            Page<Role> page = roleService.findPage(vo);
            if(page!=null){
                return new Result(true , MessageConstant.QUERY_CHECKITEM_SUCCESS , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , MessageConstant.QUERY_CHECKITEM_FAIL);
    }


    @PostMapping("add")
    public Result add(@RequestBody Role role) {
        boolean save = roleService.save(role);
        if (save) {
            return new Result(true , "添加角色成功",role);
        }
       return new Result(false , "添加角色失败");
    }

//    @PostMapping("deleteBatch")
//    public Result deleteBatch(@RequestBody List<Integer> ids) {
//        boolean b = roleService.removeByIds(ids);
//        if (b) {
//            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
//        }
//        return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
//    }
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Integer rid) {
        boolean b = roleService.del(rid);
        if (b) {
            return new Result(true, "删除角色成功");
        }
        return new Result(false, "删除角色失败");
    }


    @GetMapping("findMenu")
    @ApiOperation("查询所有菜单信息")
    public Result findMenu() {
        List<Menu> list =menuService.list();
        if (list != null) {
            return new Result(true,"查询菜单信息成功" , list);
        }
        return new Result(false,"查询菜单信息失败" );
    }

    @GetMapping("findRoleById/{id}")
    @ApiOperation("根据角色编号收集所有菜单id")
    public Result findRoleById(@PathVariable("id")  Integer roleId){
        log.info("角色编号：{}"+roleId);
        List<Integer> menuIds = roleService.findMenuByRoleId(roleId);
        if(!CollectionUtils.isEmpty(menuIds)){
            return new Result(true , "通过菜单编号查询角色信息成功" ,menuIds);
        }
        return new Result(false , "通过菜单编号查询角色信息失败");

    }

    @GetMapping("findPermissionById/{id}")
    @ApiOperation("根据角色编号收集所有权限id")
    public Result findPermissionById(@PathVariable("id")  Integer roleId){
        log.info("角色编号：{}"+roleId);
        List<Integer> permissionIds = roleService.findPermissionId(roleId);
        if(!CollectionUtils.isEmpty(permissionIds)){
            return new Result(true , "通过权限编号查询角色信息成功" ,permissionIds);
        }
        return new Result(false , "通过权限编号查询角色信息失败");
    }

    @PostMapping("addMenuToRole")
    @ApiOperation("分配菜单信息至角色")
     public  Result addMenuToRole(@RequestParam("menuIds")  Integer[] menuIds,@RequestBody Integer roleId) {
        log.info("菜单编号：{}",roleId);
      boolean flag= roleService.addMenuToRole(menuIds,roleId);
      if(flag){
          return new Result(true,"分配菜单到角色成功");
      }
      return new Result(false,"分配菜单到角色失败");
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询角色信息")
    public Result findOne(@PathVariable("id") Integer id) {
        Role role = roleService.getById(id);
        if (role != null) {
            return new Result(true, "查询角色信息成功", role);
        }
        return new Result(false, "查询角色信息失败");
    }



    @GetMapping("findPermission")
    @ApiOperation("查询所有权限信息")
    public Result findPermission() {
        List<Permission> list = permissionService.list();
        if (list != null) {
            return new Result(true, "查询权限信息成功", list);
        }
        return new Result(false, "查询权限信息失败");
    }




}
